package com.base.study.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.recy.BasicRecyViewHolder;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.commonlib.utils.SubStringUtil;
import com.base.study.R;
import com.base.study.adapter.MenuLeftAdapter;
import com.base.study.bean.OnlineStudySecondListBean;
import com.base.study.bean.OnlineStudyTreeBean;
import com.base.study.bean.OnlineStydyFirstListBean;
import com.base.study.databinding.FragmentStudyDoubleListBinding;
import com.base.study.presenter.OnlineStudyFirstListPresenter;
import com.base.study.utils.MenuLeftSpaceItemDecoration;
import com.base.study.view.OnclineStudyFirstListView;
import com.base.study.widget.RecyclerViewSpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zw on 2020/2/13.
 */

public class StudyDoubleListFragment extends CBaseMvpFragment<OnlineStudyFirstListPresenter> implements OnclineStudyFirstListView {
    private List<OnlineStudyTreeBean> menuBeanList = new ArrayList<>();
    protected FragmentStudyDoubleListBinding mBinding;
    private MenuLeftSpaceItemDecoration mCustomDividerLeft;
    private List<OnlineStudyTreeBean.children> mData = new ArrayList<>();
    private MenuLeftAdapter mMenuLeftAdapter;
    private BaseAdapter<OnlineStudyTreeBean.children> mAdapter;
    private String firstCategoryName;
    private String secondCategoryName;

    @Override
    public void init() {
        mBinding = (FragmentStudyDoubleListBinding) mRootBinding;
        mBinding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.smartRefreshLayout.setHeaderHeight(60);
        mBinding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
    }

    @Override
    public void start() {
        mPresenter = new OnlineStudyFirstListPresenter(this, getActivity());
        mPresenter.getList();
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        //LinearLayoutManager managerItems = new LinearLayoutManager(mContext);
        if (mMenuLeftAdapter == null) {
            mMenuLeftAdapter = new MenuLeftAdapter(R.layout.item_menu_left, mContext);
            mCustomDividerLeft = new MenuLeftSpaceItemDecoration();
        }

        mBinding.studyLeftList.setLayoutManager(manager);
        mBinding.studyLeftList.setAdapter(mMenuLeftAdapter);
        mBinding.studyLeftList.addItemDecoration(mCustomDividerLeft);

        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(mContext);
        //GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
        mBinding.studyRightList.setLayoutManager(linearLayoutManager2);
        mBinding.studyRightList.addItemDecoration(new RecyclerViewSpacesItemDecoration
                (10, 0, 10, 10));

        mAdapter = new BaseAdapter<OnlineStudyTreeBean.children>(mData, mContext) {
            @Override
            public void convert(BaseHolder holder, OnlineStudyTreeBean.children bean, boolean isHeader, boolean isBottom) {
                TextView title = holder.getView(R.id.name);
                title.setText(bean.getName());
                TextView des = holder.getView(R.id.des);
                des.setText(SubStringUtil.subStringCN(bean.getName(),100));
            }
        };


        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                if (mData.get(dataPosition).getType() == 0){
                    //type=0.课程分类
                    ARouter.getInstance().build("/subject/formsubject")
                            .withString("firstCategoryName",firstCategoryName)
                            .withString("secondCategoryName",mData.get(dataPosition).getName())
                            .withString("id",mData.get(dataPosition).getId()).navigation();



                } else if (mData.get(dataPosition).getType() == 1){
                    //type=1、题库
                    ARouter.getInstance().build("/sublect/list")
                            //.withString("firstCategoryName",firstCategoryName)
                            //.withString("secondCategoryName",mData.get(dataPosition).getName())
                            .withString("parentId",mData.get(dataPosition).getParentId())
                            .withString("subjectId",mData.get(dataPosition).getId())
                            .withString("parentName",firstCategoryName)
                            .withString("subjectName",mData.get(dataPosition).getName())
                            .navigation();
                }

            }
        });
        mAdapter.putNormalType(R.layout.item_online_study_second);
        //mAdapter.putNormalType(R.layout.item_online_study_round_second);
        mBinding.studyRightList.setAdapter(mAdapter);

        //左边列表点击事件
        mMenuLeftAdapter.setItemClickListener(new BasicRecyViewHolder.OnItemClickListener() {
            @Override
            public void OnItemClick(View v, int adapterPosition) {
                OnlineStudyTreeBean bean = mMenuLeftAdapter.getData(adapterPosition);
                mMenuLeftAdapter.selectTaskListItem(adapterPosition);
                mMenuLeftAdapter.notifyDataSetChanged();
                firstCategoryName = bean.getName();
                mData.clear();
                mData.addAll(bean.getChildren());
                if (bean.getChildren().size()>0){
                    mBinding.empty.setVisibility(View.GONE);
                    if (mAdapter != null) {
                        mAdapter.notifyDataSetChanged();
                    }
                }

                //mPresenter.getRightList(bean.getId());
            }
        });

        mBinding.smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.getList();
            }
        });


    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_study_double_list;
    }

    @Override
    public void returnNoData() {

    }

    @Override
    public void returnList(List<OnlineStydyFirstListBean> list) {


    }

    @Override
    public void returnTreenList(List<OnlineStudyTreeBean> list) {
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        menuBeanList.clear();
        menuBeanList.addAll(list);
        mMenuLeftAdapter.refreshDatas(menuBeanList);
        /**
         * 默认选中第一个
         */
        mMenuLeftAdapter.selectTaskListItem(0);
        mMenuLeftAdapter.notifyDataSetChanged();

        if (list != null && list.size() > 0) {
            firstCategoryName =list.get(0).getName();
            mData.clear();
            mData.addAll(list.get(0).getChildren());
            if (mData.size()>0){
                mBinding.empty.setVisibility(View.GONE);
                if (mAdapter != null) {
                    mAdapter.notifyDataSetChanged();
                }
            }else {

            }

          //  mPresenter.getRightList(list.get(0).getId());
        }
    }

    @Override
    public void returnError() {

    }


    @Override
    public void returnRightNoData() {
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void returnRightList(List<OnlineStudySecondListBean> bean) {
        mBinding.empty.setVisibility(View.GONE);
        mData.clear();
       // mData.addAll(bean);
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void returnRightMoreList(List<OnlineStudySecondListBean> bean) {

    }

    @Override
    public void returnRightNoMoreData() {

    }

    @Override
    public void returnRightError() {

    }

}
